package util;


import java.sql.*;
import java.util.Properties;

public class JdbcUtil {

    private static Properties config;

    static {
        config=new Properties();
        try {
            config.load(JdbcUtil.class
                                .getClassLoader()
                                .getResourceAsStream("dataSource.properties"));

            Class.forName(config.getProperty("jdbc.driverClassName"));

        } catch (Exception e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError("JdbcUtil初始化失败");
        }
    }


    public static Connection getConnection(){
        Connection con=null;

        try{

            con= DriverManager.getConnection(config.getProperty("jdbc.url"),
                                             config.getProperty("jdbc.username"),
                                             config.getProperty("jdbc.password"));

        }catch (Exception e){
            e.printStackTrace();
        }

        return con;
    }

    public static void close(Connection con, PreparedStatement ps, ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {

        System.out.println(getConnection());

    }

}
